package done.normal_001_100;

import org.junit.Test;

import static com.study.util.LogUtil.info;

/**
 * 8. String to Integer (atoi)
 *
 * @author wangxiaozhe
 */
@SuppressWarnings("all")
public class StringToInteger {

    public int myAtoi(String str) {
        // 190617 first
        return -1;
    }

    @Test
    public void test() {
        info("4193:{}", myAtoi("4193 dfljasdklsad asdflkjasdljkf "));
        info("-42:{}", myAtoi(" -42"));
        info(myAtoi("words and 987"));
        info(myAtoi(" "));
        info(myAtoi("-91283472332"));
        info(myAtoi("3.14159"));
        info(myAtoi("-+1"));
    }
}
































/*
public int myAtoi(String str) {
    int i = 0, j = 0, len = str.length();
    boolean negative = false;
    for (i = 0; i < len; i++) {
        if ('0' <= str.charAt(i) && str.charAt(i) <= '9') {
            break;
        } else if (str.charAt(i) == '-' || str.charAt(i) == '+') {
            negative = str.charAt(i) == '-';
            i++;
            break;
        } else if (str.charAt(i) != ' ') {
            return 0;
        }
    }
    for (j = i; j < len; j++) {
        if (str.charAt(j) < '0' || '9' < str.charAt(j)) {
            break;
        }
    }
    int ret = 0;
    String num = str.substring(i, j);
    for (int x = 0; x < num.length(); x++) {
        int cur = num.charAt(x) - '0';
        if (negative) {
            //这里判断溢出的情况和第7题一样
            if (ret < Integer.MIN_VALUE / 10|| ret == Integer.MIN_VALUE / 10 && cur > 8) {
                return Integer.MIN_VALUE;
            }
            ret = ret * 10 - cur;
        } else {
            if (ret > Integer.MAX_VALUE / 10 || ret == Integer.MAX_VALUE / 10 && cur > 7) {
                return Integer.MAX_VALUE;
            }
            ret = ret * 10 + cur;
        }
    }
    return ret;
}
*/